package com.hws.leetcode.Day_12_31;

/**
 * ClassName:TestDemo
 * Package:com.hws.leetcode.Day_12_31
 * Description:
 *
 * @ date:2021/12/31 11:28
 * @ author:hws
 */
public class TestDemo {
    int left_bound(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        int low = 0;
        int high = nums.length;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (nums[mid] == target) {
                high = mid;
            } else if (nums[mid] < target) {
                low = mid + 1;
            } else if (nums[mid] > target) {
                high = mid;
            }
        }
        if (nums.length == low) {
            return -1;
        }
        return nums[low] == target ? low : -1;
    }

    int Right_Bound(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target){
                left = mid + 1;
            }else if (nums[mid] > target){
                right = mid;
            }else if (nums[mid] < target){
                left = mid + 1;
            }
        }
        if (left == 0 ){
            return -1;
        }
        return nums[left - 1] == target ? (left - 1): -1;
    }

    public static void main(String[] args) {
        TestDemo testDemo = new TestDemo();
        int[] nums = new int[]{1, 2, 2, 2, 4};
        System.out.println(testDemo.left_bound(nums, 2));
        System.out.println(testDemo.Right_Bound(nums, 2));
    }
}
